package com.zzy.backend.service.impl;

import com.zzy.backend.mapper.UserMapper;
import com.zzy.backend.pojo.User;
import com.zzy.backend.service.impl.utils.UserDetailsImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * @author molimark<br />
 * @date: 2023/1/16 9:40<br/>
 * @description: <br/>
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userMapper.searchUserByUsername(username);
        if(user == null){
            //用户不存在会显示在登录页面，如果用户名不存在的话，spring-security自带的
            throw new RuntimeException("用户不存在");
        }
        return new UserDetailsImpl(user);
    }
}
